package com.wangsd.web.controller;

import com.wangsd.core.entity.JSONResult;
import com.wangsd.core.util.ApplicationUtils;
import com.wangsd.core.util.DateUtils;
import com.wangsd.core.util.PrintMessage;
import com.wangsd.web.model.*;
import com.wangsd.web.modelCustom.*;
import com.wangsd.web.service.CosttypeService;
import com.wangsd.web.service.DirectFeeService;
import com.wangsd.web.service.HousinginfoService;
import com.wangsd.web.service.PrintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by wangsd on 2017/12/14.
 */
@Controller
@RequestMapping("/directfee")
public class DirectFeeController {

    @Autowired
    private DirectFeeService directFeeService;
    @Autowired
    private CosttypeService costtypeService;
    @Autowired
    private PrintService printService;
    @Autowired
    private HousinginfoService housinginfoService;

    @RequestMapping("directFeeList")
    public String directFeeList(){
        return "/directfee/directFeeList";
    }

    @RequestMapping("tableDirectFee")
    @ResponseBody
    public DataTable tableDirectFee(DirectFeeCustom directFeeCustom,HttpSession session){
        DataTable dataTable = new DataTable();
        UserCustom loginUser = (UserCustom) session.getAttribute("userInfo");
        directFeeCustom.setCode(loginUser.getParentCode());
        List<DirectFeeCustom> directFeeCustoms = directFeeService.tableDirectFee(directFeeCustom);
        Integer directFeeCustomsCount = directFeeService.tableDirectFeeCount(directFeeCustom);
        dataTable.setData(directFeeCustoms);
        dataTable.setRecordsFiltered(directFeeCustomsCount);
        dataTable.setRecordsTotal(directFeeCustomsCount);
        return  dataTable;
    }

    @RequestMapping("directFeeAdd")
    public String directFeeAdd(Model model){
        List<Costtype> costList = costtypeService.queryAllList();
        model.addAttribute("costList", costList);
        return "/directfee/directFeeAdd";
    }

    @RequestMapping("saveDirectFee")
    @ResponseBody
    public JSONResult saveDirectFee(Directfee directfee, HttpSession session){
        JSONResult jsonResult = new JSONResult();
        UserCustom loginUser = (UserCustom) session.getAttribute("userInfo");
        directfee.setCode(loginUser.getParentCode());
        Integer result = directFeeService.insertSelective(directfee);
        if(result==null){
            jsonResult.setMessage("添加失败");
            jsonResult.setSuccess(false);
        }else {
            jsonResult.setMessage("添加成功");
            jsonResult.setSuccess(true);
        }
        return jsonResult;
    }

    /**
     * @Author:qc
     * @Description:
     * @param directfee
     * @Date:10:34 2017/12/28
     */
    @RequestMapping("printinfo")
    @ResponseBody
    public JSONResult printinfo(Directfee directfee,HttpSession session){
        JSONResult jsonResult = new JSONResult();
        UserCustom loginUser = (UserCustom) session.getAttribute("userInfo");
        Printinfo printinfo = printService.selectPrintinfoBydeptId(loginUser.getParentId());
        if(printinfo==null){
            jsonResult.setSuccess(false);
            jsonResult.setMessage("没有配置打印机");
            return  jsonResult;
        }else if(printinfo!=null) {
            Directfee directfeeResult = directFeeService.selectByPrimaryKey(directfee.getId());
            HousinginfoCustom housinginfo = housinginfoService.selectHousingCustomById(loginUser.getParentId());
            if(housinginfo==null){
                jsonResult.setSuccess(false);
                jsonResult.setMessage("请使用小区管理员登录");
                return  jsonResult;
            }
            PrintMessage print = new PrintMessage(printinfo.getMachineCode(), printinfo.getMsign());
            String payType = ApplicationUtils.getPayType(directfeeResult.getPayType());
            StringBuffer sb = new StringBuffer("");
            sb.append("<center>智慧小区</center>\n");
            sb.append("小区名称：" + housinginfo.getName() + "\n");
            sb.append("付款时间：" + DateUtils.formatDatetime(directfeeResult.getCreateTime()) + "\n");
            sb.append("订单编号：" + directfeeResult.getId() + "\n");
            sb.append("付款单位：" + directfeeResult.getFeeUnit() + "\n");
            sb.append("支付方式：" + payType + "\n");
            sb.append("缴费金额：" + directfeeResult.getFee() + "\n");
            sb.append("缴费明细：\n");
            sb.append("----------------------\n");
//            sb.append("<table><tr><td>账期</td><td>类别</td><td>金额</td></tr>");
            sb.append("账期：" + DateUtils.formatDatetime(directfeeResult.getCreateTime()) + "\n");
            sb.append("类别：" + directfeeResult.getProjectid() + "\n");
            sb.append("金额：" + directfeeResult.getFee() + "\n");
            /*sb.append("<tr><td>" + DateUtils.formatDatetime(directfeeResult.getCreateTime()) + "</td><td>" + directfeeResult.getProjectid() + "</td><td>" + directfeeResult.getFee() + "</td></tr>");
            sb.append("</table>\n");*/
            sb.append("----------------------\n");
            sb.append("收款单位：" + housinginfo.getParentName() + "\n");
            sb.append("----------------------\n");
            sb.append("<center>交易小票</center>\n");
            print.sendContent(sb.toString());
            jsonResult.setSuccess(true);
            jsonResult.setMessage("打印成功");
        }else {
            jsonResult.setMessage("系统异常");
            jsonResult.setSuccess(false);
        }
        return jsonResult;
    }
}
